package com.zsrt.scott.py;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.chrono.JapaneseChronology;

import com.zsrt.scott.entity.Student;

public class AA {
	
	
	public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		
		Student student = new Student();
		
		student.setId("6666");
		student.setName("yanminghao");
		student.setAge("17");
		String sql ="insert into oa_student_t(ID,NAME,AGE) VALUES(#{id},#{name},#{age})";
		
		/*int begin = sql.indexOf("#{");
		
		System.out.println(begin);
		
		
		String temp = sql.substring(begin+2);
		
		System.out.println(temp);
		
		int end = temp.indexOf("}");
		
		System.out.println(end);
		
		String pa= temp.substring(0, 4);
		System.out.println(pa);*/
		
		
		//#{
		
		
		String[] sqls = sql.split("\\#\\{");
		
		System.out.println(sqls.length);
		
		
		System.out.println("循环前------");
		
		for(int index=0; index<=sqls.length-1;index++){
			
			Object sl= sqls[index];
			
			System.out.println(sl);
			
			
		}

		
		//4 所有只需要循环3次
		System.out.println("循环后------------------");
		for(int index=1; index<=sqls.length-1;index++){
			
			String sl= (String)sqls[index];
			
			//属性的名称
			String fname = sl.substring(0, sl.length()-2);
			
			System.out.println(fname);
			String fangfa = "get"+fname.substring(0, 1).toUpperCase()+fname.substring(1);
			
			// id   getId()
			
			//拿到id属性之后  要遍历对象 student 的所有方法  找到  getId 方法  并且把调用后的值 放到原来的地方
			
			
			Class  clazz = student.getClass();
			
			
			Method[] ms = clazz.getDeclaredMethods();
			
			for(Method method: ms){
				
				String name = method.getName();
				
				
				//根据属性的名称得到方法的名称
				
				
				
				//System.out.println("name="+name + ",fangfa="+fangfa);
				
				
				if(name.equals(fangfa)){
					
					Object  object = method.invoke(student, null);
					
					
					
					
					sqls[index]=(String)object;
				}
				
			}
			
			
			
		}
		
		
		
		System.out.println("循环后------");
		
		for(int index=0; index<=sqls.length-1;index++){
			
			Object sl= sqls[index];
			
			System.out.println(sl);
			
			
		}
		
		
		//根据结果形成最终的sql
		
		StringBuffer sb = new StringBuffer();
		
		
		sb.append(sqls[0]).append(" ");
		
		
		for(int index=1;index<=sqls.length-2;index++){
			String value =sqls[index];
			
			sb.append(value).append(", ");
		}
		
		sb.append(sqls[3] + " )");
		
		System.out.println("sql= "+ sb.toString());
		
	}

}
